/**
 * 
 */
package com.yidu.menuProject.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.yidu.menuProject.action.vo.ShowComment;
import com.yidu.menuProject.action.vo.UserComment;
import com.yidu.menuProject.common.Page;
import com.yidu.menuProject.common.Tools;
import com.yidu.menuProject.dao.CommentModelMapper;
import com.yidu.menuProject.dao.IndentModelMapper;
import com.yidu.menuProject.model.CommentModel;
import com.yidu.menuProject.model.IndentModel;
/**
 * 类的描述--评论实现类
 * @author sylval
 * @date 2018年7月12日 上午11:37:14
 * @version 1.0
 */
@Service
public class CommentImpl implements CommentService{
	@Resource
	CommentModelMapper cdao;
	@Resource
	IndentModelMapper idao;
	/**
	 * 查询所有的方法
	 */
	public List<UserComment> findComment(Page page,String mealId) {
		List<UserComment> listmodel=cdao.findComment(page,mealId);
		return listmodel;
	}
	/**
	 * 查询行数的方法
	 */
	public int findTotalRows(String mealId) {
		int i=cdao.findTotalRows(mealId);
		return i;
	}
	/**
	 * 增加评论的方法
	 */
	public int insertSelective(CommentModel record) {
		record.setComId(Tools.getStringRandom(20));
		IndentModel ind= idao.selectByPrimaryKey(record.getIndId());
		record.setMealId(ind.getMealId());
		record.setUserId(ind.getUserId());
		record.setOptime(new Date());
		record.setSort(cdao.findTotalRowsWithAdd()+1);
		System.out.println("--=-=-=-=-=pll");
		int add=cdao.insertSelective(record);
		return add;
	}
	/**
	 * 前台显示评论的方法
	 */
	public List<ShowComment> SelectCommentByMeal(String mealId) {
		
		List<ShowComment> list=cdao.SelectCommentByMeal(mealId);
		
		List<ShowComment> comList= new ArrayList<ShowComment>();
		for (int i = 0; i < list.size(); i++) {
			if(i==0||(i>0&&(!(list.get(i).getCoId().equals(list.get(i-1).getCoId()))))){
				List<ShowComment> foodList = cdao.SelectCommentByCoId(list.get(i).getCoId());
				ShowComment show = new ShowComment();
				show.setUsName(list.get(i).getUsName());
				show.setCoContent(list.get(i).getCoContent());
				String food = "";
				for (int j = 0; j < foodList.size(); j++) {
					if(j==foodList.size()-1){
						food+=foodList.get(j).getFoName();
					}else{
						food+=foodList.get(j).getFoName()+",";
					}
				}
				
				show.setFoName(food);
				comList.add(show);
			}
			
			
		}
		return comList;
	}
	
	public int findTotalRowsWithAdd() {
		return cdao.findTotalRowsWithAdd();
	}
}
